import requests
from bs4 import BeautifulSoup
import os
import time
import random
# pip install fake_useragent
from fake_useragent import UserAgent

class SpiderXzmp3SingerMusics():
    def __init__(self):
        self.url = 'https://www.xzmp3.com'
        self.headers = {
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'accept-language': 'zh-CN,zh;q=0.9',
            'referer': 'https://www.xzmp3.com/',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'cross-site',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': UserAgent().chrome
        }
        self.file_output_path = './audio/xzmp3/'
    def __main__(self, singer):
        print('MP3音乐免费下载网站歌手[{}]歌曲爬虫开始执行，请稍后。。。'.format(singer))
        url = self.url + '/xiazai/{}.htm'.format(singer)
        response = requests.get(url, self.headers)
        bs = BeautifulSoup(response.content.decode(errors='ignore'), 'lxml')
        music_elements = bs.select('.list_box > .list_body > div')
        for music_element in music_elements:
            name = music_element.select_one('div > .name > a').text.replace('mp3下载', '')
            singers = music_element.select_one('div > .singer > a').text
            download_page_url = self.url + music_element.select_one('div > a')['href']
            result = self.__download__(download_page_url, name, singers)
            if result == True:
                time.sleep(random.randint(2, 4))

    def __download__(self, url, name, singer):
        response = requests.get(url, self.headers)
        bs = BeautifulSoup(response.content.decode(errors='ignore'), 'lxml')
        download_link = bs.select_one('.download > .list > div > span')['data-url']
        music_name = download_link[download_link.rfind('/')+1 : len(download_link)]
        music_name = name + '----' + singer + '.' + music_name.split('.')[-1]
        self.__create_dir__(self.file_output_path)
        music_res = requests.get(download_link, stream=True)
        source_url = music_res.url
        source_name = source_url[source_url.rfind('/')+1 : len(source_url)]
        # 确认下载链接可以访问，并且源下载链接必须是一个文件地址
        if music_res.status_code == 200 and (('.' + music_name.split('.')[-1]) in source_name) is True:
            with open(self.file_output_path + music_name, 'wb') as file:
                for chunk in music_res.iter_content():
                    file.write(chunk)
            print('歌曲----{}----下载完成！'.format(music_name))
            return True
        return False
    def __create_dir__(self, dir):
        # 获取倒数第二个/出现的位置
        last_sec_num = dir.rfind('/', 0, dir.rfind('/')-1)
        # dir的上一级目录
        dir_parent = dir[0:last_sec_num+1]
        # 判断该文件夹是否存在
        if os.path.exists(dir_parent) is False:
            # 不存在则创建
            os.mkdir(dir_parent)
            os.mkdir(dir)
        elif os.path.exists(dir) is False:
            os.mkdir(dir)

if __name__ == '__main__':
    spider = SpiderXzmp3SingerMusics()
    singer = '周杰伦'
    spider.__main__(singer)
